/*
	Copyright (C) 2010 2011 Rémi Thebault

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef TASKTREEVIEW_H
#define TASKTREEVIEW_H

#include <QMap>
#include <QString>
#include <QTreeView>
#include <QHeaderView>
#include <QStyledItemDelegate>

#include <procycle/task.h>

#include "pcchild.h"


class TaskTreeModel;


class TaskTreeItemDelegate : public QStyledItemDelegate, public PCChild
{
	Q_OBJECT

public:
	explicit TaskTreeItemDelegate(ProCycle* app, QObject* parent=0)
		:	QStyledItemDelegate(parent)
		,	PCChild(app) {}



protected:

	QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
	void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;

	QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
	void setEditorData(QWidget *editor, const QModelIndex &index) const;
	void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
	void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;

private:

	QTreeView* m_tree;
};





class TaskTreeHeader : public QHeaderView, public PCChild {
	Q_OBJECT

public:
	TaskTreeHeader(ProCycle* app, QWidget* parent=0);

	QString colName(PC::Task::Data td) {
		return m_colNames.value(td);
	}

	QSize sizeHint() const;

public slots:
	void contextMenu(QPoint);
	void userColumnSelect();

	void resizeColumns();
	void userColumnResize(int section, int oldSize, int newSize);

signals:
	void userColumnResized(PC::Task::Data, int newSize);

protected:

	void paintSection (QPainter* painter, const QRect & rect, int logicalIndex) const;


private:
	QMap<PC::Task::Data, QString>	m_colNames;
	QMap<PC::Task::Data, int>		m_colSizes;
};





class TaskTreeView : public QTreeView, public PCChild
{
	Q_OBJECT

public:
	explicit TaskTreeView(ProCycle* app, QWidget *parent=0);

	QString headerColName(PC::Task::Data td) {
		return static_cast<TaskTreeHeader*>(header())->colName(td);
	}

    void initSizePolicy();

	TaskTreeModel* taskModel();

	void expandAllTasks();
	void syncExpansion(const QModelIndex& index=QModelIndex(), bool recursive=true);

	//void selectTask(PC::Task* task);

public slots:
	void selectTask(PC::Task* selected, PC::Task* unselected);

signals:
	void taskExpansionChanged (PC::Task* task);

protected:
	void selectionChanged(const QItemSelection & selected, const QItemSelection & deselected);
	void drawBranches(QPainter *painter, const QRect &rect, const QModelIndex &index) const;

private slots:
	void nodeExpanded(const QModelIndex& index);
	void nodeCollapsed(const QModelIndex& index);

};

#endif // TASKTREEVIEW_H
